package cn.dliyy.warehouse.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.dliyy.warehouse.bean.BGoods;
import cn.dliyy.warehouse.bean.BUnit;
import cn.dliyy.warehouse.bean.Category;
import cn.dliyy.warehouse.bean.SStorage;
import cn.dliyy.warehouse.service.BGoodsService;
import cn.dliyy.warehouse.service.CategoryService;
import cn.dliyy.warehouse.service.SStorageService;
import cn.dliyy.warehouse.service.UnitService;
import cn.dliyy.warehouse.service.WarehouseService;

@Controller
@RequestMapping(value = "/storage")
public class SStorageController {

  @Resource
  private SStorageService sStorageService;
  @Resource
  private SStorage sStorage;
  @Resource
  private BGoodsService bGoodsService;
  @Resource
  private CategoryService categoryService;
  @Resource
  private UnitService unitService;
  @Resource
  private WarehouseService warehouseService;

  @RequestMapping(value = { "", "/" })
  public String load(ModelMap modelMap) {
    List<?> list = sStorageService.getList();
    modelMap.put("list", list);
    return "/storage/storage";
  }

  @RequestMapping(value = "/add", method = RequestMethod.GET)
  public String create(ModelMap modelMap) {
    sStorage = sStorageService.get();
    modelMap.addAttribute("storage", sStorage);
    modelMap.addAttribute("reasonMap", sStorage.getReasonMap());
    modelMap.addAttribute("warehouseList", warehouseService.getList());
    return "/storage/storage_add";
  }

  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public String create(@Valid @ModelAttribute("storage") SStorage sStorage, BindingResult result, ModelMap modelMap) {
    if (result.hasErrors()) {
      sStorage = sStorageService.get();
      modelMap.addAttribute("reasonMap", sStorage.getReasonMap());
      modelMap.addAttribute("warehouseList", warehouseService.getList());
      return "/storage/storage_add";
    }
    sStorageService.save(sStorage);
    return "redirect:/storage/";
  }

  @RequestMapping(value = { "/ajax/", "/ajax" })
  @ResponseBody
  public Map<String, Object> load(@RequestParam(value = "query") String name) {
    List<Object> bgoodlist = (List<Object>) bGoodsService.getListByName(name);
    if (bgoodlist == null || bgoodlist.size() == 0) {
      return null;
    }
    List<String> suggestions = new ArrayList<String>();
    List<SStorage> data = new ArrayList<SStorage>();
    Map<String, Object> map = new HashMap<String, Object>();
    for (int i = 0; i < bgoodlist.size(); i++) {
      StringBuffer s = new StringBuffer();
      BGoods bg = (BGoods) bgoodlist.get(i);
      Category tcate = (Category) categoryService.get(Category.class, bg.getCategoryId());
      BUnit tunit = (BUnit) this.unitService.get(bg.getUnitId());
      s.append(bg.getName());
      s.append(" ");
      s.append(bg.getSpecification());
      s.append(" ");
      s.append(tcate.getName());
      suggestions.add(s.toString());

      SStorage tsStorage = sStorageService.getLatest();// 获取最近一次的设置
      tsStorage.setCategory(tcate);// 更新为本次设置，此时的items等内容为最近一次的设置
      tsStorage.setUnit(tunit);
      tsStorage.setGoods(bg);

      data.add(tsStorage);
    }
    map.put("query", name);
    map.put("suggestions", suggestions);
    map.put("data", data);
    // return
    // "{query:'m',suggestions:['螺栓 M4','螺栓1 M4','aaaa M3'],data:['1','2','3']}";
    return map;
  }

  @RequestMapping(value = { "/ajax/goods"})
  @ResponseBody
  public SStorage load(@RequestParam(value = "name") String name, @RequestParam(value = "spec") String specification) {
    BGoods bg = bGoodsService.get(name, specification);
    if (bg == null) {
      return null;
    }

    Category tcate = (Category) categoryService.get(Category.class, bg.getCategoryId());
    BUnit tunit = (BUnit) this.unitService.get(bg.getUnitId());

    SStorage tsStorage = new SStorage();
    tsStorage.setCategory(tcate);
    tsStorage.setUnit(tunit);
    tsStorage.setGoods(bg);

    return tsStorage;
  }
}
